<?php

namespace app\common\model;

use think\Collection;

/**
 * Class Consulting
 * @package app\model
 * @property int $id
 * @property  int $categoryId
 * @property int $memberId
 * @property int $lawyerId
 * @property int $robotId
 * @property int $initMatchType 原匹配方式
 * @property int $matchType 当前匹配方式
 * @property string $businessType 业务类型
 * @property int $consultingType
 * @property int $consultingServerId
 * @property int $consultingServerSkuId
 * @property int $status
 * @property int $isEnd
 * @property int $isEntry
 * @property int $isHidden
 * @property int $lawsuitId
 * @property float $lawyerMoney
 * @property int $isAsked
 * @property int $questionsNum
 * @property int $questionsNumRemind
 * @property int $isChatFreely
 * @property int $isChatFreelyEndTime
 * @property string $ask
 * @property int $questionId
 * @property int $freeNum
 * @property int $lastChatTime
 * @property int $memberLastTime
 * @property int $lawyerLastTime
 * @property int $memberHiddenTime
 * @property int $lawyerHiddenTime
 * @property int $isComment
 * @property int $commentLevel
 * @property int $comment
 * @property int $isAnswered
 * @property int $rushStatus
 * @property int $rushTime
 * @property int $rushExpTime
 * @property int $rushTimes
 * @property int $responseTimeLength
 * @property int $provinceId
 * @property int $cityId
 * @property int $lastActiveTime
 * @property string $aryStatus
 * @property int $aryTime
 * @property string $aryCode
 * @property string $aryMsg
 * @property string $pf
 * @property string $chain
 * @property int $promotionGroupId
 * @property int $updated
 * @property int $created
 * @property ConsultingMessage $lastMessage
 * @property Member $lawyer
 * @property Member $member
 * @property Robot $robot
 * @property ConsultingQuestionAnswer $questionAnswer
 * @property Collection $messages
 * @property LawCaseCategory $category
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class Consulting extends \app\common\model\BaseModel
{

    // businessType 业务类型
    // BT_CONSULTING 普通咨询(v1.0版本咨询) BT_RANDOM 极速咨询(随机匹配) BT_DESIGNATED 找律师（指定人选）
    const BT_CONSULTING = 'consulting';
    const BT_RANDOM = 'random';
    const BT_DESIGNATED = 'designated';

    // aryStatus 案如云推送状态
    // UNPUSH 未推送 PUSHING 推送中 PUSHED 已推送 PUSH_FAILED
    const UNPUSH = ConsultingQuestion::UNPUSH;
    const PUSHING = ConsultingQuestion::PUSHING;
    const PUSHED = ConsultingQuestion::PUSHED;
    const PUSH_FAILED = ConsultingQuestion::PUSH_FAILED;

    // matchType 当前律师匹配方式 0系统随机匹配 1用户指定 2律师抢单 3管理员挖掘 4律师买案源来的 5推送给案如云处理 6SVIP派单
    const MATCH_TYPE_RANDOM = '0';
    const MATCH_TYPE_DESIGNATE = '1';
    const MATCH_TYPE_RUSH = '2';
    const MATCH_TYPE_ARY = '5';
    const MATCH_TYPE_SVIP = '6';

    protected $name = 'Consulting';

    /**
     * 更新最后活动时间
     * @param $lastActiveTime
     * @return void
     */
    public function updateActiveTime($lastActiveTime)
    {
        $this->where('id', $this->id)
            ->where('lastActiveTime', '<', $lastActiveTime)
            ->update(['lastActiveTime' => $lastActiveTime]);
    }

    // 案源
    public function lawCase()
    {
        return $this->hasOne(LawCase::class, 'questionId', 'questionId');
    }

    // 用户
    public function member()
    {
        return $this->belongsTo(Member::class, 'memberId', 'id');
    }

    // 律师
    public function lawyer()
    {
        return $this->hasOne(Member::class, 'id', 'lawyerId');
    }

    // 咨询机器人
    public function robot()
    {
        return $this->hasOne(Robot::class, 'id', 'robotId');
    }

    // 消息
    public function messages()
    {
        return $this->hasMany(ConsultingMessage::class, 'consultingId', 'id');
    }

    // 最新的一条消息
    public function lastMessage()
    {
        return $this->hasOne(ConsultingMessage::class, 'consultingId', 'id');
    }

    // 未读的消息
    public function unreadMessages()
    {
        return $this->hasMany(ConsultingMessage::class, 'consultingId', 'id');
    }

    // 律师发的消息
    public function lawyerMessages()
    {
        return $this->hasMany(ConsultingMessage::class, 'consultingId', 'id');
    }

    // 咨询创建时的服务
    public function consultingServer()
    {
        return $this->belongsTo(ConsultingServer::class, 'consultingServerId', 'id');
    }

    // 咨询创建时的初始服务的补贴
    public function consultingSubsidy()
    {
        return $this->hasOne(ConsultingSubsidy::class, 'consultingId', 'id');
    }

    // 案件类型
    public function category()
    {
        return $this->belongsTo(LawCaseCategory::class, 'categoryId', 'id');
    }

    // 提问的回答
    public function questionAnswer()
    {
        return $this->hasOne(ConsultingQuestionAnswer::class, 'consultingId', 'id');
    }

    public function province()
    {
        return $this->hasOne(Area::class, 'id', 'provinceId');
    }

    public function city()
    {
        return $this->hasOne(Area::class, 'id', 'cityId');
    }

    // 订单
    public function trades()
    {
        return $this->hasMany(Trade::class, 'consultingId', 'id');
    }

    // 订单-退款中
    public function refundingTrades()
    {
        return $this->hasMany(Trade::class, 'consultingId', 'id');
    }
    /**
     * 1对1
     * @return \think\model\relation\HasOne
     */
    //    public function one(){
    //        return $this->hasOne( Test1::class, 'Test1表中的键', 'Consulting表中的键');
    //    }

    /**
     * 1对多
     * @return \think\model\relation\HasMany
     */
    //    public function many(){
    //        return $this->hasMany( Test1::class, 'Test1表中的键', 'Consulting表中的键');
    //    }

    /**
     * 多对多
     * @return \think\model\relation\BelongsToMany
     */
    //    public function belongs(){
    //        return $this->belongsToMany( Test1::class, '中间表', '中间表中Test1的键', '中间表中Consulting的键');
    //    }
}
